import { useDictionary } from "@/utils/useTable";

export const checkCommon = (
  searchParam: object | null,
  reset: Function | null
) => {
  const getEffectTypeCode = (row) => {
    const findOperateType = useDictionary("CheckOperateType").find(
      (item: any) => item.value === row.opeTypeCode
    );
    if (findOperateType) {
      const findEffectType = useDictionary(findOperateType.value).find(
        (item: any) => item.value === row.effectTypeCode
      );
      if (findEffectType) {
        return findEffectType.label;
      } else {
        return useDictionary("CheckOperateType", row.effectTypeCode);
      }
    } else return row.effectTypeCode;
  };
  const effectTypeCodeList = ref([]);

  const changeOpeType = (val) => {
    if (!searchParam) return;
    searchParam.value.effectTypeCode = "";
    if (val !== null && val !== undefined && val !== "") {
      const filterDictList = useDictionary(val);
      if (filterDictList.length > 0) effectTypeCodeList.value = filterDictList;
      else {
        effectTypeCodeList.value = [
          useDictionary("CheckOperateType").find(
            (item: any) => item.value == val
          ),
        ];
      }
    } else effectTypeCodeList.value = [];
  };

  const resetHandle = () => {
    effectTypeCodeList.value = [];
    reset?.();
  };

  const handlePictureCardPreview = (url: string) => {
    url = url?.replace("fileCenter/download/", "fileCenter/preview/");
    window.open(url);
  };

  return {
    effectTypeCodeList,
    getEffectTypeCode,
    changeOpeType,
    resetHandle,
    handlePictureCardPreview,
  };
};
